package com.will.maze;

import java.util.Random;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

public class DrawView extends View{

	Display display;
	private Paint p;
	static int width,height;
	private static Square square;
	private static Wall wall1,wall2,wall3,wall4;
	MazeActivity ma;
	int randomRect = 0;
	public boolean isGravOn = true;
	Integer score = 0;
	public boolean isOneDone = false;
	boolean isDone = false;
	
	public DrawView(Context context) {
		super(context);
		score = 0;
		BitmapFactory.Options bo = new BitmapFactory.Options();
		bo.inJustDecodeBounds=true;
		WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
		display = wm.getDefaultDisplay();
		width = display.getWidth();
		height = display.getHeight();
		square = new Square(context,width/2-10,height/2-10);
		wall1 = new Wall(context,50,height/2-150);
		wall2 = new Wall(context,width/2-150,height/4*3-10);
		wall3 = new Wall(context,width-60,height/2-150);
		wall4 = new Wall(context,width/2-150,height/4-10);
	}
	
	public void changeWall(int w){
		randomRect = w;
	}
	
	public void onDraw(Canvas c){
		p = new Paint();
		p.setAntiAlias(true);
		//c.drawColor(Color.DKGRAY);
		
		if(isGravOn){
			//square.setXDirection((int)-ma.x);
			square.setXDirection((int)-ma.x);
			square.setYDirection((int)ma.y);
			}
		
		
		if(randomRect == 0){
			//draw rect 1
			
			
			if(isDone){p.setColor(Color.YELLOW);}
			if(!isDone){p.setColor(Color.GREEN);}
			c.drawRect(wall1.getX(), wall1.getY(), wall1.getX()+Wall.wallHeight, wall1.getY()+Wall.wallWidth, p);
			
			if(square.x<=wall1.x+ Wall.wallHeight && square.x >= wall1.x && square.y+20>=wall1.y && square.y<=wall1.y + Wall.wallWidth){
				isGravOn=false;
				//square.setX(wall1.x+Wall.wallHeight);
				square.setXDirection(0);
				square.setYDirection((int)ma.y);
				if((int)-ma.x < -6 ){
					if(!isOneDone){score++;
					MazeActivity.score.setText(score.toString());
					isOneDone = true;
					}
					isDone = true;
				}
			}else{isGravOn=true;isOneDone=false;isDone = false;}
			
		}
		
		if(randomRect == 1){
			if(isDone){p.setColor(Color.YELLOW);}
			if(!isDone){p.setColor(Color.GREEN);}
			c.drawRect(wall2.getX(), wall2.getY(), wall2.getX()+Wall.wallWidth, wall2.getY()+Wall.wallHeight, p);
			//draw rect 2
			if(square.y + 20>=wall2.y && square.y - 20 <= wall2.y && square.x -20 >= wall2.x && square.x <= wall2.x + Wall.wallWidth){
				isGravOn=false;
				//square.setY(wall2.y-20);
				square.setYDirection(0);
				square.setXDirection((int)-ma.x);
				if((int)ma.y>6){
					if(!isOneDone){score++;
					MazeActivity.score.setText(score.toString());
					isOneDone = true;
					}
					isDone = true;
				}
		}
		else{isGravOn=true;isOneDone = false;isDone = false;}
			
		}	
			
		
		if(randomRect == 2){
			//draw rect 3
			if(isDone){p.setColor(Color.YELLOW);}
			if(!isDone){p.setColor(Color.GREEN);}
			c.drawRect(wall3.getX(), wall3.getY(), wall3.getX()+Wall.wallHeight, wall3.getY()+Wall.wallWidth, p);
			if(square.x + 20>=wall3.x && square.x - 20<=wall3.x + Wall.wallHeight && square.y - 20>=wall3.y && square.y<=wall3.y + Wall.wallWidth){
				isGravOn=false;
				//square.setX(wall3.x-20);
				square.setXDirection(0);
				square.setYDirection((int)ma.y);
				if((int)-ma.x>-6){
					if(!isOneDone){score++;
					MazeActivity.score.setText(score.toString());
					isOneDone = true;
					}
					isDone = true;
				}
				
			}else{isGravOn=true;isOneDone = false;isDone = false;}
			
		}
		
		if(randomRect == 3){
			
			//draw rect 4
				if(isDone){p.setColor(Color.YELLOW);}
				if(!isDone){p.setColor(Color.GREEN);}
			c.drawRect(wall4.getX(), wall4.getY(), wall4.getX()+Wall.wallWidth, wall4.getY()+Wall.wallHeight, p);
			if(square.y<=wall4.y + Wall.wallHeight && square.y>=wall4.y && square.x - 20>=wall4.x && square.x <= wall4.x + Wall.wallWidth){
				isGravOn=false;
				//square.setY(wall4.y + Wall.wallHeight);
				square.setYDirection(0);
				square.setXDirection((int)-ma.x);
				if((int)ma.y>-6){
					if(!isOneDone){score++;
					MazeActivity.score.setText(score.toString());
					isOneDone = true;
					}
					isDone = true;
				}
				
			}else{isGravOn=true;isOneDone = false;isDone = false;}
			
		}
		
		//Draw the PLAYER
		p.setColor(Color.CYAN);
		c.drawRect(square.getX(), square.getY(), square.getX()+square.getSLength(), square.getY()+square.getSLength(),p);
		square.move(square.x,square.y,0,square);
		//End//
		//Invalidate
		invalidate();
	}
		

	public void updateScore(){
		if(!isOneDone){
			score--;
			MazeActivity.score.setText(score.toString());
			isDone = false;
		}
	}
	public void setWidth(int nW){
		width = nW;
	}
	
	public void setHeight(int nH){
		width = nH;
	}
	
	public int getSWidth(){
		return width;
	}
	
	public int getSHeight(){
		return height;
	}
	
}
